package com.xudow.app.dynamicstate_old.module.discovery.search;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;

import com.jude.beam.bijection.RequiresPresenter;
import com.jude.beam.expansion.list.BeamListFragment;
import com.jude.beam.expansion.list.ListConfig;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.jude.utils.JUtils;
import com.xudow.app.R;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;
import com.xudow.app.dynamicstate_old.module.discovery.viewholder.DynamicContentViewHolder;
import com.xudow.app.dynamicstate_old.module.discovery.viewholder.PostLongViewHolder;
import com.xudow.app.dynamicstate_old.module.discovery.viewholder.PostNormalViewHolder;
import com.xudow.app.dynamicstate_old.module.discovery.viewholder.RepostViewHolder;

/**
 * Created by zhuchenxi on 16/6/24.
 */
@RequiresPresenter(DynamicSearchPresenter.class)
public class DynamicSearchFragment extends BeamListFragment<DynamicSearchPresenter, Dynamic> {

    private static final int POST_NORMAL = 1;
    private static final int POST_LONG = 2;
    private static final int REPOST = 3;

    public void search(String text) {
        getPresenter().word = text;
//        getPresenter().getAdapter().clear();
        getListView().showProgress();
        getPresenter().onRefresh();
    }

    @Override
    public ListConfig getConfig() {
        return super.getConfig().setRefreshAble(false);
    }

    @Override
    public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
        DynamicContentViewHolder viewHolder = null;
        if (viewType == POST_LONG) {
            viewHolder = new PostLongViewHolder(getPresenter().getAdapter(), parent);
        } else if (viewType == POST_NORMAL) {
            viewHolder = new PostNormalViewHolder(getPresenter().getAdapter(), parent);
        } else if (viewType == REPOST) {
            viewHolder = new RepostViewHolder(getPresenter().getAdapter(), parent);
        }
        return viewHolder;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getListView().addItemDecoration(new DividerDecoration(getResources().getColor(R.color.gray100), JUtils.dip2px(16)));
    }

    @Override
    public int getViewType(int position) {
        Dynamic dynamic = getPresenter().getAdapter().getItem(position);
        // 1：长文，2：普通
        int type = dynamic.getType();
        // 上级动态主键, 为0说明，这条动态不是转发的
        long pId = dynamic.getpId();
        if (pId != 0)
            return REPOST;
        else if (type == 1)
            return POST_LONG;
        else if (type == 2)
            return POST_NORMAL;
        return 0;
    }
}
